public class add6_22 {
    public static double sqrt(long n) {
        if (n == 0) {
            return 0;
        }
        double lastGuess = 1.0;
        double nextGuess;
        double difference;
        do {
            nextGuess = (lastGuess + n / lastGuess) / 2;
            difference = Math.abs(nextGuess - lastGuess);
            lastGuess = nextGuess;
        } while (difference >= 0.0001);
        return nextGuess;
    }

    public static void main(String[] args) {
        long[] testNumbers = {4, 25, 100, 2, 7};
        int j;
        for (j = 0; j < testNumbers.length; j++) {
            long num = testNumbers[j];
            double approxSqrt = sqrt(num);
            double actualSqrt = Math.sqrt(num);
            System.out.printf("Approximate sqrt(%.0f): %.4f, Actual sqrt: %.4f\n",
                    (double)num, approxSqrt, actualSqrt);
        }
    }
}